<?php

use yii\helpers\Html;
use common\entities\cms\DirEntity;
?>
<script src="/static_public/js/lib/ueditor/ueditor.config.js"></script>
<script src="/static_public/js/lib/ueditor/ueditor.all.min.js"></script>

<style>
    .section{
        width:270px;
        margin-left: 10px;
        float:left;
        line-height: 40px;
    }
    .form-w{
        width:170px;
        display:inline;
    }
    .section-span{
        width:100px;
        float:left;
    }
</style>
    <div class="section-table mt20">
        <div class="table-header lh30 pb5 clearfix">
            <span class="fl f14">文章编辑</span>
            <div class="fr btn-group">
                <button class="btn" id="save">保存</button>
                <button class="btn btn-yellow" id="save_create">保存并新建</button>
                <button class="btn btn-yellow" id="save_close">保存并关闭</button>
                <?php if( ! empty($a_id)){?>
                <button class="btn btn-yellow" id="save_copy">保存并复制</button>
                <?php }?>
                <button class="btn" id="no_save">取消</button>
            </div>
        </div>
        <div class="mt10">
            <div style="width:70%;float:left;">
                <script type="text/plain" id="meEditor"><?php echo $content;?></script>
            </div>
            <div class="float:left;">
                <div class="section"><div class="section-span">标题：</div><div><input type="text" value="<?php echo $title;?>" name="title" id="title" class="form-control form-w"></div></div>
                <div class="section"><div class="section-span">tag：</div><div><input type="text" value="<?php echo $tag;?>" name="tag" id="tag" class="form-control form-w"></div></div>
                <div class="section"><div class="section-span">缩略图：</div><div><input type="text" value="<?php echo $image_url;?>" name="image_url" id="image_url" class="form-control form-w"></div></div>
                <div class="section"><div class="section-span">分类：</div>
                <div>
                <?php echo Html::dropDownList('dir_id', $dir_id, DirEntity::getOptions(), ['class' => 'form-control form-w', 'id'=>'dir_id']); ?>
                </div>
                </div>    
                <div class="section"><div class="section-span">附件1名称：</div><div><input type="text" value="<?php echo $file_name;?>" name="file_name1" id="file_name" class="form-control form-w"></div></div>
                <div class="section"><div class="section-span">附件1地址：</div><div><input type="text" value="<?php echo $file_url;?>" name="file_url1" id="file_url" class="form-control form-w"></div></div>
                <div class="section"><div class="section-span">附件2名称：</div><div><input type="text" value="<?php echo $file_name2;?>" name="file_name2" id="file_name2" class="form-control form-w"></div></div>
                <div class="section"><div class="section-span">附件2地址：</div><div><input type="text" value="<?php echo $file_url2;?>" name="file_url2" id="file_url2" class="form-control form-w"></div></div>
                <div class="section"><div class="section-span">附件3名称：</div><div><input type="text" value="<?php echo $file_name3;?>" name="file_name3" id="file_name3" class="form-control form-w"></div></div>
                <div class="section"><div class="section-span">附件3地址：</div><div><input type="text" value="<?php echo $file_url3;?>" name="file_url3" id="file_url3" class="form-control form-w"></div></div>

                <input type="hidden" name="a_id" id="a_id" value="<?php echo $a_id;?>">
                
            </div>

        </div>
    </div>

</div></div>
<script type="text/javascript" charset="utf-8">
    var op = {
        initialFrameHeight: 300
    };    
    window.UEDITOR_CONFIG.toolbars = [[
            'fullscreen', 'source', '|', 'undo', 'redo', '|',
            'bold', 'italic', 'underline', 'fontborder', 'strikethrough', 'superscript', 'subscript', 'removeformat', 'formatmatch', 'autotypeset', 'blockquote', 'pasteplain', '|', 'forecolor', 'backcolor', 'insertorderedlist', 'insertunorderedlist', 'selectall', 'cleardoc', '|',
            'rowspacingtop', 'rowspacingbottom', 'lineheight', '|',
            'customstyle', 'paragraph', 'fontfamily', 'fontsize', '|',
            'directionalityltr', 'directionalityrtl', 'indent', '|',
            'justifyleft', 'justifycenter', 'justifyright', 'justifyjustify', '|', 'touppercase', 'tolowercase', '|',
            'link', 'unlink', 'anchor', '|', 'imagenone', 'imageleft', 'imageright', 'imagecenter', '|',
            'simpleupload', 'insertimage', 'emotion', 'scrawl', 'insertvideo', 'music', 'attachment', 'map', 'gmap', 'insertframe', 'insertcode', 'webapp', 'pagebreak', 'template', 'background', '|',
            'horizontal', 'date', 'time', 'spechars', 'snapscreen', 'wordimage', '|',
            'inserttable', 'deletetable', 'insertparagraphbeforetable', 'insertrow', 'deleterow', 'insertcol', 'deletecol', 'mergecells', 'mergeright', 'mergedown', 'splittocells', 'splittorows', 'splittocols', 'charts', '|',
            'print', 'preview', 'searchreplace', 'help', 'drafts'
        ]];
    var editor = new UE.ui.Editor(op);
    editor.render('meEditor');  
//    editor.ready(function(){    
//        editor.setContent("");    
//    });

    var article = {
        ajax :function($url, $type, $obj){
            article.type = $type;
            $.ajax({
                url:$url,
                data:sData(),
                type:'post',
                success:submitSuccess
            })  
            function sData () {
                $data = {
                    'title':$('#title').val(),
                    'tag':$('#tag').val(),
                    'imageUrl':$('#image_url').val(),
                    'fileUrl':$('#file_url').val() + "@@" +  $('#file_url2').val() + "@@" +  $('#file_url3').val(),
                    'fileName':$('#file_name').val() + "@@" +  $('#file_name2').val() + "@@" +  $('#file_name3').val(),
                    'content':UE.getEditor('meEditor').getContent(),
                    'dirId':$('#dir_id').val(),
                    'aId':$('#a_id').val()
                };
                return $data;
            }   
            function submitSuccess(data) {
                if (data.retMsg != null && data.retMsg != "") {
                    alert(data.retMsg);
                } else {
                    switch (article.type) {
                        case 1:
                            alert('保存成功！');
                            $('#a_id').val(data.data);
                            break;
                        case 2:
                            alert('保存成功！');
                            window.location.href = "/api/index.php?r=/cms/article/edit";
                            break;
                        case 3:
                            alert('保存成功！');
                            window.location.href = "/api/index.php?r=/cms/article/get-list";
                            break;
                        case 4:
                            alert('保存并复制成功！');
                            window.location.href = "/api/index.php?r=/cms/article/edit&id="+data.data ;
                            break;
                        default:
                            break;
                    }
                }
            }
        },
        type:0,
        bind:function(){
            $('#save').bind('click',function(){article.ajax('/api/index.php?r=/cms/article/save', 1);});
            $('#save_create').bind('click',function(){article.ajax('/api/index.php?r=/cms/article/save', 2);});
            $('#save_close').bind('click',function(){article.ajax('/api/index.php?r=/cms/article/save', 3);});
            $('#save_copy').bind('click',function(){article.ajax('/api/index.php?r=/cms/article/save&copy=1', 4);});
            $('#no_save').bind('click',function(){window.location.href = "/api/index.php?r=/cms/article/get-list";});
        }
    }
    
    article.bind();
    
</script>